package me.zhengjie.ppService;


import lombok.RequiredArgsConstructor;
import me.zhengjie.Enum.InBoundType;
import me.zhengjie.Enum.RequestResponseType;
import me.zhengjie.pojo.PPInBoundAllowGroup;
import me.zhengjie.pojo.RequestResponseGroup;
import me.zhengjie.modbusTCP.EntranceTCP;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@RequiredArgsConstructor
public class EntranceTcpService {

    private final EntranceTCP entranceTCP;


    /*public void writeInBoundAllowType(int isAllow, String stationName) {
        Map<String, PPInBoundAllowGroup> map = new HashMap<>();
        //把枚举中的9个寄存器信息存到map中
        for (InBoundType x : InBoundType.values()) {
            map.put(x.getPpInBoundAllowGroup().getStationName(), x.getPpInBoundAllowGroup());
        }

        //筛选符合的工作站
        entranceTCP.write(map.get(stationName).getInBoundAllowAdress(), isAllow, 5);
    }


    public int readInBoundAllowType(String stationName) {
        Map<String, PPInBoundAllowGroup> map = new HashMap<>();
        //把枚举中的9个寄存器信息存到map中
        for (InBoundType x : InBoundType.values()) {
            map.put(x.getPpInBoundAllowGroup().getStationName(), x.getPpInBoundAllowGroup());
        }

        //筛选符合的工作站
        return entranceTCP.readHoldingRegisters(map.get(stationName).getReadPpPalletAdress());
    }*/



    public void writeRequestResponseType(int isAllow, String stationName, int type) {
        Map<String, RequestResponseGroup> map = new HashMap<>();
        //把枚举中的9个寄存器信息存到map中
        for (RequestResponseType x : RequestResponseType.values()) {
            map.put(x.getGroup().getStationName(), x.getGroup());
        }

        //筛选符合的工作站
        //type类型0-出库，1入库
        if (type == 0) {
            entranceTCP.write(map.get(stationName).getOutBoundRequest(), isAllow, 5);
        } else {
            entranceTCP.write(map.get(stationName).getInBoundRequest(), isAllow, 5);
        }


    }


    public int readRequestResponseType(String stationName, int type) {
        Map<String, RequestResponseGroup> map = new HashMap<>();

        for (RequestResponseType x : RequestResponseType.values()) {
            map.put(x.getGroup().getStationName(), x.getGroup());
        }

        //筛选符合的工作站
        //type类型0-出库，1入库
        if (type == 0) {
            return entranceTCP.readHoldingRegisters(map.get(stationName).getOutBoundResponse());
        } else {
            return entranceTCP.readHoldingRegisters(map.get(stationName).getInBoundResponse());
        }
    }



}
